#include <bits/stdc++.h>

using namespace std;

const int N = 100010;
class Solution
{
public:
    int f[N][3][2];
    int maxProfit(vector<int> &prices)
    {
        memset(f, -0x3f, sizeof(f));
        for (int i = 0; i < prices.size(); ++i)
        {
            f[i][0][0] = 0;
        }
        for (int i = 0; i < prices.size(); ++i)
        {
            for (int j = 1; j <= 2; ++j)
            {
                f[i][j][0] = max(f[i - 1][j][0], f[i - 1][j][1] + prices[i]);
                f[i][j][1] = max(f[i - 1][j][1], f[i - 1][j - 1][0] - prices[i]);
            }
        }

        int ret = 0;
        for (int j = 1; j <= 2; ++j)
        {
            ret = max(ret, f[prices.size() - 1][j][0]);
        }
        return ret;
    }
};